package com.example.ylsys.plants.presenter;

import android.content.Context;
import android.widget.Toast;

import com.example.ylsys.plants.base.BasePresenter;
import com.example.ylsys.plants.bean.BackResultBean;
import com.example.ylsys.plants.bean.LoginParamsBean;
import com.example.ylsys.plants.bean.LoginUserBean;
import com.example.ylsys.plants.contract.LoginDialogContract;
import com.example.ylsys.plants.model.LoginDialogModel;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class LoginDialogPresenter extends BasePresenter<LoginDialogContract.View>implements LoginDialogContract.Presenter{

    private LoginDialogContract.Login_Model model;
    public LoginDialogPresenter(){
        model=new LoginDialogModel();
    }

    @Override
    public void getAc(Map<String, String> url_map, Map<String, String> post_map) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }model.ac_Api(url_map,post_map).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<BackResultBean>() {
                @Override
                public void accept(BackResultBean backResultBean) throws Exception {
                    mView.getAcOnSuccess(backResultBean);
                }
            });
    }

    @Override
    public void login(Map<String, String> url_map, Map<String, String> post_map) {
        if (!isViewAttached()) {
            return;
        }
        model.login_Api(url_map,post_map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BackResultBean>() {
                    @Override
                    public void accept(BackResultBean backResultBean) throws Exception {
                        mView.loginOnSuccess(backResultBean.getResult());
                    }
                }, new Consumer<Throwable>() {
                               @Override
                               public void accept(Throwable throwable) throws Exception {
                                   mView.onError(throwable);
                               }
                           });
    }

    @Override
    public void SaveData(LoginParamsBean loginParamsBean,Context context) {
        if (!isViewAttached()) {
            return;
        }
        model.save_Data(loginParamsBean.getTel(),loginParamsBean.getCypher(),context);

    }


}
